<template>
  <div class="echart" id="mychart" style="width: 100%; height: 100%" @mouseenter="handleMouseEnter"
    @mouseleave="handleMouseLeave"></div>
</template>

<script>
import * as echarts from "echarts";
import { ShouyeZongHeYuJing } from "../../https/api/user";
export default {
  data() {
    return {
      list: [],
      time: "",
    };
  },
  mounted() {
    this.initEcharts();
    this.ZongHeYuJing();
    this.time = setInterval(() => {
      this.ZongHeYuJing();
    }, 5000);
  },
  methods: {
    initEcharts() {
      // 基本柱状图
      const option = {
        xAxis: {
          data: this.list.map(i => i.Name),
          axisPointer: {
            type: "shadow",
          },
          axisLine: {
            lineStyle: {
              // 设置x轴颜色
              color: "rgb(242,148,85)",
            },
          },
          axisLabel: {
            rotate: -25, //代表逆时针旋转
          },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
            shadowStyle: {
              color: "rgba(145, 145, 145, 0.3)",
            },
          },
        },
        grid: {
          top: 35,
          left: 30,
          right: 30,
          bottom: 35,
        },
        yAxis: {
          name: "报警(次)",
          axisLine: {
            lineStyle: {
              // 设置y轴颜色
              color: "#87CEFA",
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "#87CEFA",
              type: "dashed",
            },
          },
        },
        series: [
          {
            type: "bar", //形状为柱状图
            data: this.list.map(i => i.count),
            barWidth: 15, //柱子宽度

            label: {
              show: true, // 显示标签
              textStyle: {
                fontSize: 12, // 设置文字大小为16px
              },
            },
          },
        ],
      };
      const myChart = echarts.init(document.getElementById("mychart"));
      myChart.setOption(option);
      //随着屏幕大小调节图表
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    },

    ZongHeYuJing() {
      ShouyeZongHeYuJing().then((res) => {
        console.log("首页综合预警", res);
        if (res) {
          this.list = res
        }
        this.initEcharts();
      });
    },
    handleMouseEnter() {
      clearInterval(this.time);
    },
    handleMouseLeave() {
      this.time = setInterval(() => {
        this.ZongHeYuJing();
      }, 1000);
    },
  },
  beforeDestroy() {
    clearInterval(this.time);
  },
};
</script>
